package richbar.com.richbar.activity.me.myassets;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.common.base.BaseActivity;
import com.common.util.ToastUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import richbar.com.richbar.R;
import richbar.com.richbar.activity.login.LoginAc;
import richbar.com.richbar.bean.P;
import richbar.com.richbar.bean.RebateBean;
import richbar.com.richbar.bean.ShopCartListBean;
import richbar.com.richbar.util.LoadingDialog;
import richbar.com.richbar.util.NetWorkUtil;
import richbar.com.richbar.view.linear.LinearLayoutAdapter;
import richbar.com.richbar.view.linear.LinearView;


/**
 * Created by Administrator on 2018/11/29.
 * 待返金额
 */

public class AmountReturnedAc extends BaseActivity {
    @BindView(R.id.linearView_bottom)
    LinearView linearView_bottom;//使用规则
    private MyBottomAdapter myBottomAdapter;
    private LoadingDialog dialog;
    List<P> dataTop = new ArrayList<>();
    @BindView(R.id.tv_balance)
    TextView tv_balance;//使用规则

    @Override
    public int initLayout() {
        return R.layout.ac_amount_returned;
    }

    @Override
    public void initTitle() {

    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        dialog = new LoadingDialog(AmountReturnedAc.this, 0);
    }

    @Override
    public void initData() {
        dialog.show();
        NetWorkUtil.getRebate(new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                dialog.dismiss();
                final RebateBean rebateBean = MyGson.getInstance().fromJson(data, RebateBean.class);
                if (rebateBean.getData().getList() != null && rebateBean.getData().getList().size() > 0) {
                    for (int i = 0; i < rebateBean.getData().getList().size(); i++) {
                        dataTop.add(new P(rebateBean.getData().getList().get(i).getName(), rebateBean.getData().getList().get(i).getTime(), "返利:￥" + rebateBean.getData().getList().get(i).getPrice()));
                    }
                    myBottomAdapter = new MyBottomAdapter(AmountReturnedAc.this, dataTop);
                    linearView_bottom.setAdapter(myBottomAdapter);
                    tv_balance.setText(rebateBean.getData().getBalance());
                }
                ToastUtil.showToast(AmountReturnedAc.this,rebateBean.getMsg());
            }

            @Override
            public void onError(String msg) {
                super.onError(msg);
                dialog.dismiss();
                ToastUtil.showToast(AmountReturnedAc.this,msg);
            }
        });
    }

    class MyBottomAdapter extends LinearLayoutAdapter<P> {
        public MyBottomAdapter(Context context, List<P> list) {
            super(context, list);
        }

        @Override
        protected View getView(List<P> data, P bean, final int position) {
            View v = getLayoutInflater().inflate(R.layout.item_ar, null);
            TextView tv_title = v.findViewById(R.id.tv_title);
            TextView tv_time = v.findViewById(R.id.tv_time);
            TextView tv_price = v.findViewById(R.id.tv_price);
            View line = v.findViewById(R.id.line);
            tv_title.setText(bean.getTitle());
            tv_time.setText(bean.getTime());
            tv_price.setText(bean.getPrice());
            if (position == data.size() - 1) {
                line.setVisibility(View.VISIBLE);
            }
            return v;
        }
    }
}
